package com.sunnada.edu.system.sysrole.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.sysmodule.bean.Module;
import com.sunnada.edu.system.sysrole.bean.Role;
import com.sunnada.edu.system.sysrole.service.RoleMethods;
import com.sunnada.edu.system.sysrole.service.RoleService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/**
 * explain 角色控制层 
 * @author zjc 2016-8-6
 * version 1.0
 */
@Controller
@RequestMapping("/roleController")
public class RoleController extends BasicController{
	@Resource
	RoleService roleService;
	/**角色首页*/
	@RequestMapping("/role")
	public String role(HttpServletRequest request) { 
		String moduleId=request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "system/sysrole/role";
	}
	/**角色查询*/
	@RequestMapping("/roleSearch")
	public void roleSearch(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Role.class, params);
		params.put("loginUserId", "admin");
		Page page = new Page(); 
		page.setRowCount(roleService.getRoleCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = roleService.findRoles(params);
		JSONObject json = new JSONObject();		
		json.put("list", userList);
		json.put("params", params); 
		printInfo(response,json);
	}
	/**角色添加页面*/
	@RequestMapping("/roleAddShow")
	public String roleAddShow(HttpServletRequest request) { 
		return "system/sysrole/role_add";
	}
	/**角色保存页面*/
	@RequestMapping("/roleSave")
	public void roleSave(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Role.class, params);
		String recId = params.get("recId"); 		
		if (Utils.isNull(recId)) {
			msg = roleService.roleAdd(params);
		}else{
			params.put("recIds",Utils.getSqlSplitStr(recId));
			msg = roleService.roleUpdate(params);
		}	
		printInfo(response,msg);
	}

	/**角色修改页面*/
	@SuppressWarnings("rawtypes")
	@RequestMapping("/roleUpdateShow")
	public String roleUpdateShow(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Role.class, params);
		Map role = roleService.getRoleByRecId(params);
		String s[]=roleService.getModuleIdsByRoleId(params);
		request.setAttribute("moduleIds", s[0]);
		request.setAttribute("moduleNames", s[1]);
		request.setAttribute("role", role);
		return "system/sysrole/role_update";
	}
	/**角色删除功能 */
	@RequestMapping("/roleDelete")
	public void roleDelete(HttpServletRequest request,HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds",Utils.getSqlSplitStr(recIds));
		msg = roleService.roleDel(params);	
		printInfo(response,msg);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	/**角色树:全展开*/
	@RequestMapping("/roleTree2")
	public void roleTree2(HttpServletResponse response,HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Module.class, params);
		List list=roleService.roleTree2(params);
		JSONObject map = new JSONObject();
		map.put("list", list);
		printInfo(response,map);
	}
	
	
	/**授权树：全展开 ,同步加载 */
	@RequestMapping("/roleAuthorizeTree")
	public void roleAuthorizeTree(HttpServletResponse response,HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Role.class, params);
		List list=roleService.roleAuthorizeTree(params);
		JSONObject map = new JSONObject();
		map.put("list", list);
		printInfo(response,map);
	}
	
	
	
	/**角色启用 zjc 2015-5-26*/
	@RequestMapping("/roleEnable")
	public void roleEnable(HttpServletRequest request,HttpServletResponse response){
		if(RoleMethods.isAdminRole(request)==false){
			msg = "无此操作权限！";
		}else{
		  HashMap<String, String>params=new HashMap<String, String>();
		  String recIds=request.getParameter("recIds");
		  params.put("recIds", Utils.getSqlSplitStr(recIds));
		  msg=roleService.roleEnable(params);
		}
	  printInfo(response,msg);
	}
	/**角色停用  zjc 2015-5-26*/
	@RequestMapping("/roleDisable")
	public void roleDisable(HttpServletRequest request,HttpServletResponse response){
		if(RoleMethods.isAdminRole(request)==false){
			msg = "无此操作权限！";
		}else{
		HashMap<String,String> params=new HashMap<String, String>();
		String recIds=request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg=roleService.roleDisable(params);
		}
		printInfo(response, msg);
	}
	
	
	/**角色绑定用户页面  zjc 2015-5-20*/
	@RequestMapping("/roleBind")
	public String roleBind(HttpServletRequest request) { 
		HashMap<String, String> params = new HashMap<String, String>();
		String recId=request.getParameter("recId");
		params.put("recId",recId);
		Map role = roleService.getRoleByRecId(params);
		String roleName= role.get("ROLE_NAME").toString();
		request.setAttribute("recId", recId);
		request.setAttribute("roleName", roleName);
		return "system/role_bind";
	}
	/**根据角色ID查用户数据  zjc 2015-5-21*/
	@RequestMapping("roleUserSearchById")
	public void roleUserSearchById(HttpServletRequest request,HttpServletResponse response) {
		//String recId=request.getParameter("recId"); //角色recId
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Role.class, params);
		Page page = new Page(); 
		page.setRowCount(roleService.getRoleUserCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = roleService.findRoleUsers(params);
		JSONObject json = new JSONObject();		
		json.put("list", userList);
		json.put("params", params); 
		printInfo(response,json);
	}
	/**根据角色ID查询不存在该角色的用户数据 zjc 2015-5-22*/
	@RequestMapping("roleUserSearchNoexistById")
	public void roleUserSearchNoexistById(HttpServletRequest request,HttpServletResponse response) {
		//String recId=request.getParameter("recId"); //角色recId
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Role.class, params);
		Page page = new Page(); 
		page.setRowCount(roleService.getUserNoexistRoleIdCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = roleService.findUserNoexistRoleIds(params);
		JSONObject json = new JSONObject();		
		json.put("list", userList);
		json.put("params", params); 
		printInfo(response,json); 
	}
	/**用户绑定角色 zjc 2015-5-22*/
	@RequestMapping("roleBindSave")
	public void roleBindSave(HttpServletRequest request,HttpServletResponse response){
		HashMap<String, String> params=new HashMap<String, String>();
	    String userIds=request.getParameter("userIds");
	    String roleId=request.getParameter("roleId");
		params.put("userIds", userIds);
		params.put("roleId", roleId);
		msg=roleService.roleBindSave(params);
		printInfo(response, msg);
	}
	/**用户解绑角色 zjc 2015-5-25*/
	@RequestMapping("roleBindDelete")
	public void roleBindDelete(HttpServletRequest request,HttpServletResponse response){
		HashMap<String, String> params=new HashMap<String, String>();
	    String userIds=request.getParameter("userIds");
	    String roleId=request.getParameter("roleId");
		params.put("userIds",Utils.getSqlSplitStr(userIds));
		params.put("roleId", roleId);
		msg=roleService.roleBindDelete(params);
		printInfo(response, msg);
	}
}
